<?php
class editAction extends sfActions
{
	public function execute($request)
	{
		$this->advertisement_id = $request->getParameter('advertisement_id');
		if ($this->advertisement_id) {
			$this->ads = Doctrine_Core::getTable('GlAdvertisement')->find($this->advertisement_id);
			if ($this->ads)
				$this->ads_form = new AdminAdvertisementForm($this->ads);
		}
		
		if ($request->hasParameter('ads')) {
			$form = $request->getParameter('ads');
			$this->status = $this->updateAdvertisementFromRequest($this->ads, $form);
			if ($this->status) {
				try {
					$files = $this->getRequest()->getFiles();
					$file = $files['ads']['picture'];
					if ($file['size'] > 0)
					{
						$tmpName = $file['tmp_name'];
						$fileName = $file['name'];
						$fileExt = trim(strrchr($fileName,'.'));
		
						$img = new sfImage($tmpName, $file['type']);
						$img->saveAs(sfConfig::get('sf_upload_dir').'/ads/'.$fileName, $file['type']);
						
						$img1 = new sfImage(sfConfig::get('sf_upload_dir').'/ads/'.$fileName, $file['type']);
						$fileName1 = ($this->ads->advertisement_id).$fileExt;
						$img1->thumbnail(170,235,'center');
						$img1->setQuality(100);
						$img1->saveAs(sfConfig::get('sf_upload_dir').'/ads/'.$fileName1, $file['type']);
						
						$img3 = new sfImage(sfConfig::get('sf_upload_dir').'/ads/'.$fileName, $file['type']);
						$fileName2 = "slide_".$fileName1;
						$img3->thumbnail(154,234,'center');
						$img3->setQuality(100);
						$img3->saveAs(sfConfig::get('sf_upload_dir').'/ads/'.$fileName2, $file['type']);
						
						$this->ads->picture = $fileName1;
					}
				} catch (sfException $e) {
					//echo $e->getMessage();
				}
				
				$this->ads->save();
				
				$this->redirect('advertisement/index');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updateAdvertisementFromRequest(&$ads, $form)
	{	
		if ($form['name'])
			$ads->name = $form['name'];
		if ($form['link'])
			$ads->link = $form['link'];
		if ($form['display_position'])
			$ads->display_position = $form['display_position'];
		if ($form['is_active']) {
			if ($form['is_active'] == 'no')
				$ads->is_active = false;
			else
				$ads->is_active = true;
		}
		if ($form['content'])
			$ads->content = $form['content'];
		if ($form['position'])
			$ads->position = $form['position'];

		$ads->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>